package me.hys.service.doctor.impl;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import me.hys.base.ApiResult;
import me.hys.base.PageQuery;
import me.hys.bo.doctor.RvtUserBo;
import me.hys.cons.ApiStatus;
import me.hys.dao.doctor.IReservationMapper;
import me.hys.dao.system.IUserMapper;
import me.hys.entity.doctor.ReservationEntity;
import me.hys.entity.system.UserEntity;
import me.hys.service.doctor.IReservationManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-23 下午11:36:30 . <br>
 * Function: 预约管理
 */
@Service
@Transactional
class ReservationManagerImpl implements IReservationManager {
	@Autowired
	private IUserMapper userMapper;
	@Autowired
	private IReservationMapper reservationMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.IReservationManager#findPage(me.hys.base.PageQuery)
	 */
	@Override
	public PageQuery<RvtUserBo> findPage(PageQuery<RvtUserBo> page) {
		Map<String, Object> condition = page.getCondition();
		List<RvtUserBo> list = reservationMapper.findPageBo(condition);
		page.setList(list);
		page.setTotal(reservationMapper.findPageBoCount(condition));
		// 总页数
		int size = (int) Math.ceil(page.getTotal() * 1.0f / page.getLimit());
		page.setSize(size);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.IReservationManager#findPageBo(me.hys.base.PageQuery
	 * )
	 */
	@Override
	public PageQuery<RvtUserBo> findPageBo(PageQuery<RvtUserBo> page) {
		Map<String, Object> condition = page.getCondition();
		String userid = String.valueOf(condition.get("userid"));
		UserEntity user = userMapper.findById(userid);
		if (null == user) {
			return page;
		}
		condition.put("type", user.getType());
		if ("history".equals(condition.get("history"))) {
			condition.put("history", 1);
		} else {
			condition.put("history", 0);
		}
		List<RvtUserBo> list = reservationMapper.findPageBo(condition);
		page.setList(list);
		page.setTotal(reservationMapper.findPageBoCount(condition));
		// 总页数
		int size = (int) Math.ceil(page.getTotal() * 1.0f / page.getLimit());
		page.setSize(size);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.IReservationManager#saveOrUpdate(me.hys.entity.
	 * doctor.ReservationEntity)
	 */
	@Override
	public ApiResult saveOrUpdate(ReservationEntity entity) {
		if (entity.getId() > 0) {
			reservationMapper.update(entity);
		} else {
			entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
			reservationMapper.save(entity);
		}
		return new ApiResult(ApiStatus.SUCCESS, "操作成功");
	}
}
